<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { ChallengeApi } from '@/api/controller';
  import { Toastloading, ToastWarning, ToastSuccess } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import serachUserid from '@/components/serachUserid/index.vue';
  const formLabelWidth = '130px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogTableVisible:false,
    dialogFormVisible:false,
    disabled:false,
    gameName:'',
    list:[] as Array<any>,
  })
  interface formQuert {
    white_id:number
    user_id:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    user_id: [
      { required: true, message: '请输入用户UUID查询', trigger: 'blur' }
    ],
  }
  const handleForm = ()=>{
    action.dialogFormVisible = true
  }
  const handleDelete = (row:any = {})=>{
    const { id=0, uuid='' } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm(`确认删除UUID：${uuid}用户白名单的配置吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      ChallengeApi.whiteUserDelete({id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=>{
    const loading = Toastloading()
    const res = await ChallengeApi.whiteUserList({
      white_id:form.white_id
    })
    loading.close()
    const { data=[] } = res
    action.list = data
  }
  
  const openDialog = async(query:any = {})=> {
    const { id, gameName } = query
    if(!id) return ToastWarning('缺少白名单ID')
    form.white_id = id
    await getList()
    action.gameName = gameName
    action.dialogTableVisible = true
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      await ChallengeApi.whiteUserAdd(form).then(res=>{
        ToastSuccess('新增成功!')
        loading.close()
        closeDialog()
        getList()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const closeDialog = ()=>{
    form.user_id = ''
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogTableVisible"
    width="55%"
    :title="`${action.gameName}-白名单ID:${form.white_id}用户配置`">
      <div class="xm-headTab">
        <div class="labelBox">
          <div class="labelItem">
            <el-button type="primary" @click="handleForm()">新增</el-button>
          </div>
        </div>
      </div>
      <el-table
        class="xm-main xm-TableCenter"
        :data="action.list"
        border>
        <el-table-column width="100" prop="id" label="id" />
        <el-table-column width="100" prop="uuid" label="UUID" />
        <el-table-column width="100" prop="user_id" label="user_id" />
        <el-table-column min-width="80" prop="nickname" label="用户名称" />
        <el-table-column width="120" fixed=right label="操作">
          <template #default="scope">
            <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog
      top="5vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="55%"
      title="新增白名单用户">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="用户UUID" prop="user_id">
          <serachUserid v-model:user_id="form.user_id" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>